Python for TensorFlow Pocket Primer by Oswald Campesato

Python for TensorFlow Pocket Primer by Oswald Campesato

Author:Oswald Campesato
Language: eng
Format: mobi, epub, pdf
Tags: python
Publisher: Mercury Learning and Information
Published: 2019-08-15T04:00:00+00:00


FIGURE 4.8 A Colored Square in a Grid of Line Segments.

RANDOMIZED DATA POINTS IN MATPLOTLIB

Listing 4.9 displays the contents of lin-reg-plot.py that illustrates how to plot a graph of random points.

LISTING 4.9: lin-plot-reg.py

import numpy as np

import matplotlib.pyplot as plt

trX = np.linspace(-1, 1, 101) # Linear space of 101 and [-1,1]

#Create the y function based on the x axis

trY = 2*trX + np.random.randn(*trX.shape)*0.4+0.2

#create figure and scatter plot of the random points

plt.figure()

plt.scatter(trX,trY)

# Draw one line with the line function

plt.plot (trX, .2 + 2 * trX)

plt.show()

Listing 4.9 defines the NumPy variable trX that contains 101 equally spaced numbers that are between -1 and 1 (inclusive). The variable trY is defined in two parts: the first part is 2*trX and the second part is a random value that is partially based on the length of the one-dimensional array trX. The variable trY is the sum of these two “parts,” which creates a “fuzzy” line segment. The next portion of Listing 4.9 creates a scatterplot based on the values in trX and trY, followed by the Pyplot API plot() that renders a line segment.

Figure 4.9 displays a random set of points based on the code in Listing 4.9.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.